package cn.flying.cloud.flow.service.impl;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.flying.cloud.base.common.utils.Ft;
import cn.flying.cloud.base.common.vo.PageRequest;
import cn.flying.cloud.base.common.vo.PageResponse;
import cn.flying.cloud.flow.bo.FwProcAttributeBo;
import cn.flying.cloud.flow.entity.FwProcAttribute;
import cn.flying.cloud.flow.mapper.FwProcAttributeMapper;
import cn.flying.cloud.flow.service.FwProcAttributeService;

/**
 * 流程属性信息表 服务实现类
 *
 * @author admin
 * @since 2025-09-24 22:32:54
 */
@Service
public class FwProcAttributeServiceImpl extends ServiceImpl<FwProcAttributeMapper, FwProcAttribute> implements FwProcAttributeService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 查询总数
     *
     * @param param
     * @return
     */
    @Override
    public long countObj(FwProcAttributeBo param) {
        return this.count(getLambdaQueryWrapper(param));
    }

    /**
     * 分页查询流程属性信息表数据
     *
     * @param request
     * @return
     */
    @Override
    public PageResponse<FwProcAttributeBo> getPageList(PageRequest<FwProcAttributeBo> request) {
        FwProcAttributeBo param = request.getParam();
        LambdaQueryWrapper<FwProcAttribute> wrapper = getLambdaQueryWrapper(param);
        // wrapper TODO
        Page<FwProcAttribute> page = this.page(new Page<>(request.getPageNum(), request.getPageSize()), wrapper);
        return new PageResponse<>(page.getTotal(), (int) page.getCurrent(), page.getRecords().stream().map(FwProcAttribute::toBO).collect(Collectors.toList()));
    }

    /**
     * 构建查询对象
     *
     * @param param
     * @return
     */
    private LambdaQueryWrapper<FwProcAttribute> getLambdaQueryWrapper(FwProcAttributeBo param) {
        LambdaQueryWrapper<FwProcAttribute> wrapper = new LambdaQueryWrapper<>();
        if (param != null) {
            wrapper.eq(FwProcAttribute::getProcDefId, param.getProcDefId());
        }
        return wrapper;
    }

    /**
     * 列表查询流程属性信息表数据
     *
     * @param param
     * @return
     */
    @Override
    public List<FwProcAttributeBo> getList(FwProcAttributeBo param) {
        LambdaQueryWrapper<FwProcAttribute> wrapper = getLambdaQueryWrapper(param);
        // wrapper TODO
        List<FwProcAttribute> list = this.list(wrapper);
        return list.stream().map(FwProcAttribute::toBO).collect(Collectors.toList());
    }

    /**
     * 查询流程属性信息表详情数据
     *
     * @param param
     * @return
     */
    @Override
    public FwProcAttributeBo getOneObj(FwProcAttributeBo param) {
        LambdaQueryWrapper<FwProcAttribute> wrapper = getLambdaQueryWrapper(param);
        if (wrapper.isEmptyOfWhere()) {
            return new FwProcAttributeBo();
        }
        return this.getOne(wrapper).toBO();
    }

    /**
     * 根据Id查询流程属性信息表详情数据
     *
     * @param id
     * @return
     */
    @Override
    public FwProcAttributeBo getObjById(Serializable id) {
        return this.getById(id).toBO();
    }

    /**
     * 新增流程属性信息表数据
     *
     * @param param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean createObj(FwProcAttributeBo param) {
        param.setProcDefKey(param.getProcDefId().split(":")[0]);
        param.setTenantId(Ft.Local.TenantId());
        FwProcAttribute obj = new FwProcAttribute();
        obj.fromBO(param);
        return this.save(obj);
    }

    /**
     * 更新流程属性信息表数据
     *
     * @param param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateObj(FwProcAttributeBo param) {
        param.setProcDefKey(param.getProcDefId().split(":")[0]);
        FwProcAttribute obj = new FwProcAttribute();
        obj.fromBO(param);
        return this.saveOrUpdate(obj);
    }

    /**
     * 删除流程属性信息表数据
     *
     * @param param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteObj(FwProcAttributeBo param) {
        LambdaQueryWrapper<FwProcAttribute> wrapper = getLambdaQueryWrapper(param);
        // wrapper TODO
        return this.remove(wrapper);
    }

    /**
     * 根据Id删除流程属性信息表数据
     *
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteById(Serializable id) {
        return this.removeById(id);
    }
}
